package com.zmc.common.handler;

import com.zmc.common.enums.ErrorCodeEnum;
import com.zmc.common.exception.base.BusinessException;
import com.zmc.common.exception.base.ThirdBaseException;
import com.zmc.common.exception.base.WebDisplayException;
import com.zmc.common.exception.file.FileNameLengthLimitExceededException;
import com.zmc.common.exception.file.FileSizeLimitExceededException;
import com.zmc.common.exception.file.InvalidExtensionException;
import com.zmc.common.exception.token.TokenExpireException;
import com.zmc.common.utils.R;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author：zmc
 * @Date：2024/10/30 10:25
 */

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {


    @ExceptionHandler(value = FileNameLengthLimitExceededException.class)
    public R fileNameLengthLimitExceededExceptionHandler(FileNameLengthLimitExceededException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

    @ExceptionHandler(value = FileSizeLimitExceededException.class)
    public R fileSizeLimitExceededExceptionHandler(FileSizeLimitExceededException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

    @ExceptionHandler(value = InvalidExtensionException.class)
    public R invalidExtensionExceptionHandler(InvalidExtensionException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

    @ExceptionHandler(value = TokenExpireException.class)
    public R tokenExpireExceptionHandler(TokenExpireException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

    /**
     * 字段验证统一处理
     * */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R validExceptionHandler(MethodArgumentNotValidException ex) {
        StringBuilder sb = new StringBuilder();
        ex.getBindingResult().getAllErrors().forEach(error ->{
            sb.append(error.getDefaultMessage()).append("，");
        });
        sb.deleteCharAt(sb.length() - 1);
        return R.error(ErrorCodeEnum.FIELD_VALID_EXCEPTION.getCode(), sb.toString());
    }

    /**
     * 三方调用异常统一处理
     * */
    @ExceptionHandler(value = ThirdBaseException.class)
    public R thirdBaseExceptionHandler(ThirdBaseException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

    /**
     * 业务异常
     * */
    @ExceptionHandler(value = BusinessException.class)
    public R businessExceptionHandler(BusinessException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

    /**
     * 页面显示异常
     * */
    @ExceptionHandler(value = WebDisplayException.class)
    public R webDisplayExceptionHandler(WebDisplayException exception) {
        return R.error(exception.getErrorCode().getCode(), exception.getMessage());
    }

}
